﻿
namespace Test
 module Loops = 

    open System
    open System.Windows
    open System.Windows.Media
    open System.Windows.Shapes
    open System.Windows.Controls
    open FsSignals.Signal

    let makeLoopColorPanel () = 

        let colorPanel = new DockPanel()
        colorPanel.Height <- 100.0
        colorPanel.HorizontalAlignment <- HorizontalAlignment.Stretch
        let colorPanelBorder = new Border()
        colorPanelBorder.BorderThickness <- new Thickness(2.0)
        colorPanelBorder.BorderBrush <- Brushes.Black
        colorPanelBorder.Child <- colorPanel

        let pushB = new Button()
        pushB.Content <- "Push Me"
        let resetB = new Button()
        resetB.Content <- "Reset"
    
        let sp1 = new StackPanel()
        sp1.Margin <- new Thickness(10.0)
        sp1.Children.Add(colorPanelBorder)
        sp1.Children.Add(pushB)
        sp1.Children.Add(resetB)

        let colors = 
            [
                Brushes.Red
                Brushes.Blue
                Brushes.Green
            ]

        let rec engine() =
            colorPanel.Background <- Brushes.White
            let pushSig = sourceEvent pushB.Click 
            let resetSig = sourceEvent resetB.Click 
            let s1 = loopSig colors pushSig
            let s2 = s1 =>> (fun c -> colorPanel.Background <- c)
            switch s2 (resetSig ==>> engine)

        engine();
        sp1


    let makeIterColorPanel () = 

        let colorPanel = new DockPanel()
        colorPanel.Height <- 100.0
        colorPanel.HorizontalAlignment <- HorizontalAlignment.Stretch
        let colorPanelBorder = new Border()
        colorPanelBorder.BorderThickness <- new Thickness(2.0)
        colorPanelBorder.BorderBrush <- Brushes.Black
        colorPanelBorder.Child <- colorPanel

        let pushB = new Button()
        pushB.Content <- "Push Me"
        let resetB = new Button()
        resetB.Content <- "Reset"
    
        let sp1 = new StackPanel()
        sp1.Margin <- new Thickness(10.0)
        sp1.Children.Add(colorPanelBorder)
        sp1.Children.Add(pushB)
        sp1.Children.Add(resetB)

        let colors = 
            [
                Brushes.Red
                Brushes.Blue
                Brushes.Green
            ]

        let rec engine() =
            colorPanel.Background <- Brushes.White
            let pushSig = sourceEvent pushB.Click 
            let resetSig = sourceEvent resetB.Click 
            let (s1, notification) = iterNotifySig colors pushSig
            let s2 = s1 =>> (fun c -> colorPanel.Background <- c)
            switch s2 (whenSig notification ==>> engine)

        engine();
        sp1

    let makeScanPanel () = 


        let pushB = new Button()
        pushB.Content <- "Push Me"
        let resetB = new Button()
        resetB.Content <- "Reset"
        let textBox = new TextBox()
    
        let sp1 = new StackPanel()
        sp1.Margin <- new Thickness(10.0)
        sp1.Children.Add(pushB)
        sp1.Children.Add(resetB)
        sp1.Children.Add(textBox)


        let rec engine() =
            textBox.Text <- "result"
            let pushSig = sourceEvent pushB.Click --> 1.0
            let resetSig = sourceEvent resetB.Click 
            let s1 = pushSig |> scan (fun s v -> s+v) 0.0
            let s2 = s1 =>> (fun v -> textBox.Text <- sprintf "%A" v)
            switch s2 (resetSig ==>> engine)

        engine();
        sp1


